/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camera;

import UserInterface.FinalGUI;
import coordinates.ConvexHull;
import coordinates.MeanDetector;
import java.util.logging.Level;
import java.util.logging.Logger;
import objectExtraction.HSVSkinDetector;
import utilities.AngleFinder;
import utilities.FindContours;
import com.googlecode.javacv.FrameGrabber.Exception;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;


public class SkinBasedCamera implements Runnable {

    final int CAMERA_NUM; // Default camera for this time
    final int CAMERA_FOV;//angle ofthe camera in degrees
    private int IMAGE_NO;
    AngleFinder angFind = new AngleFinder();
    MeanDetector meanDet = new MeanDetector();
//    CanvasFrame canvas = new CanvasFrame("Web Cam Live");
//    CanvasFrame HSVCanvas = new CanvasFrame("Skin Detection");
    private double theta, theta1;
    FrameGrabber grabber;
    IplImage hsvSkin;
    HSVSkinDetector hd = new HSVSkinDetector();
    FindContours fc = new FindContours();
    ConvexHull ch = new ConvexHull();
    CvSeq largestCont;
    CvSeq secondLargestCont;
    CvPoint fingerTip;
    public int rel_case;
    int prevX = 0, prevY = 0;
    IplImage imTemp;
    IplImage skinTemp;
    boolean active=true;
   // FinalGUI gui = new FinalGUI();

    public SkinBasedCamera() {
        CAMERA_NUM = 0;
        CAMERA_FOV = 88;
     //   canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    }

    public SkinBasedCamera(int camera, int fov) {
        CAMERA_NUM = camera;
        CAMERA_FOV = fov;
     //   canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    }

    @Override
    public void run() {
        //     String file = "resources/fypVid" + CAMERA_NUM + ".avi";
        IMAGE_NO = FinalGUI.initializeCamera();
        grabber = new VideoInputFrameGrabber(CAMERA_NUM);
        int posX;
        try {
            grabber.start();
            
            Thread.sleep(1000);
            IplImage img;

            while (active) {
                img = grabber.grab();
                imTemp = img.clone();

                //   canvas.showImage(img);
                if (img != null) {

                    hsvSkin = hd.getHSVSkin(img);
                    //cvReleaseImage(img);
                    skinTemp = hsvSkin.clone();
                    //  HSVCanvas.showImage(hsvSkin);
                    
                    rel_case = fc.findLargeContours(hsvSkin);

                    if (rel_case == 2) {

                        largestCont = fc.getLargestContour();
                        fingerTip = ch.getFingerTip(largestCont);
                        cvDrawCircle(imTemp, fingerTip, 5, CvScalar.RED, -1, 8, 0);
                        FinalGUI.setCamera(imTemp, skinTemp, IMAGE_NO);
                        posX = fingerTip.x();
                       //  System.out.println(CAMERA_NUM + " cam posX= " + posX);
                        theta = angFind.angleFinder(posX, hsvSkin.width(), CAMERA_FOV);

                        cvClearSeq(largestCont);
                        cvReleaseImage(hsvSkin);



//                        secondLargestCont = fc.getSecondLargestContour();
//                        fingerTip = ch.getFingerTip(secondLargestCont);
//                        posX = fingerTip.x();
//                        theta1 = angFind.angleFinder(posX, hsvSkin.width(), CAMERA_FOV);

                        // Code to calculate the relevant case of the gesture

                    } else if (rel_case == 1) {

                        largestCont = fc.getLargestContour();
                        fingerTip = ch.getFingerTip(largestCont);
                         cvDrawCircle(imTemp, fingerTip, 15, CvScalar.RED, -1, 8, 0);
                        FinalGUI.setCamera(imTemp, skinTemp, IMAGE_NO);
                        posX = fingerTip.x();
                        //System.out.println(CAMERA_NUM + " cam posX= " + posX);
                        theta = angFind.angleFinder(posX, hsvSkin.width(), CAMERA_FOV);

                        cvClearSeq(largestCont);
                        cvReleaseImage(hsvSkin);

                    } else {
                        theta = 0;
                          FinalGUI.setCamera(imTemp, skinTemp, IMAGE_NO);
                        cvReleaseImage(hsvSkin);
                    }
                        active=FinalGUI.isThreadActive();
                }

            }
        } catch (InterruptedException ex) {
            Logger.getLogger(SkinBasedCamera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {

            System.out.println("Exception in Skin Based Detector");
            e.printStackTrace();
        }
    }

    /**
     * @return the theta
     */
    public double getTheta() {
        return theta;
    }

    public double getTheta1() {
        return theta1;
    }
}
